#! /usr/bin/python
# -*- coding: cp1250 -*-

import re, string, sys

def wcount(argv):
    """
    """
    filename = argv[1]; lower = 1; upper = 1000000000
    if len(argv) == 3: lower = int(argv[2])
    if len(argv) == 4: lower = int(argv[2]); upper = int(argv[3])
        
    opf = open(filename[:-4] + '.words.out', 'w')
    inf = open(filename, 'r')
    data = inf.read()
    inf.close()
    
    data = string.lower(data)
    data = re.sub('[^a-zA-Z]', ' ', data)
    data = re.sub(' {2,}', ' ', data)
    
    words = string.split(data, ' ')
    
    res = {}
    for word in words:
        if res.has_key(word):
            res[word] += 1
        else:
            res[word] = 1

    for k in res.keys():
        cnt = res[k]
        if (lower <= cnt) and (cnt <= upper):
            opf.write("%s %s\n" % (string.zfill(cnt, 4), k))
    opf.close()

if __name__ == '__main__':
    ''' '''
    wcount(sys.argv)
